package com.zhss.demo.register.server;

/**
 * 这个controller是负责接收register-client发送过来的请求的
 * 在Spring Cloud Eureka中用的组件是jersey，百度一下jersey是什么东西
 * 在国外很常用jersey，restful框架，可以接受http请求
 * @author gubatian
 * @date 2021/9/7 5:49 下午
 */
public class RegisterServerController {
    /**
     * 获取注册表
     */
    private ServiceRegistry serviceRegistry = ServiceRegistry.getInstance();

    /**
     * 注册服务
     * @param request 注册请求
     * @return 注册响应
     */
    public RegisterResponse register(RegisterRequest request) {
        RegisterResponse response = new RegisterResponse();
        try {
            ServiceInstance serviceInstance = new ServiceInstance();
            serviceInstance.setServiceName(request.getServiceName());
            serviceInstance.setServiceInstanceId(request.getServiceInstanceId());
            serviceInstance.setIp(request.getIp());
            serviceInstance.setHostname(request.getHostname());
            serviceInstance.setPort(request.getPort());

            serviceRegistry.register(serviceInstance);
            response.setStatus(RegisterResponse.SUCCESS);

            // 更新自我保护机制的阈值
            synchronized(SelfProtectionPolicy.class) {
                SelfProtectionPolicy selfProtectionPolicy = SelfProtectionPolicy.getInstance();
                selfProtectionPolicy.setExpectedHeartbeatRate(
                        selfProtectionPolicy.getExpectedHeartbeatRate() + 2);
                selfProtectionPolicy.setExpectedHeartbeatThreshold(
                        (long)(selfProtectionPolicy.getExpectedHeartbeatRate() * 0.85));
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(RegisterResponse.FAILURE);
        }
        return response;
    }

    /**
     * 发送心跳
     * @param request 心跳请求
     * @return 心跳响应
     */
    public HeartbeatResponse heartbeat(HeartbeatRequest request) {
        HeartbeatResponse response = new HeartbeatResponse();
        try {
            ServiceInstance serviceInstance = serviceRegistry.getRegistry()
                    .get(request.getServiceName()).get(request.getServiceInstanceId());
            serviceInstance.renew();

            // 心跳计数
//            HeartbeatMessuredRate heartbeatMessuredRate = HeartbeatMessuredRate.getInstance();
//            heartbeatMessuredRate.increment();
            HeartbeatCounter heartbeatCounter = HeartbeatCounter.getInstance();
            heartbeatCounter.increment();

            response.setStatus(HeartbeatResponse.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            response.setStatus(HeartbeatResponse.FAILURE);
        }

        return response;
    }

    /**
     * 服务下线
     * @param serviceName 服务名称
     * @param serviceInstanceId 服务实例ID
     */
    public void cancel(String serviceName, String serviceInstanceId) {
        serviceRegistry.remove(serviceName, serviceInstanceId);

        // 更新自我保护机制的阈值
        synchronized(SelfProtectionPolicy.class) {
            SelfProtectionPolicy selfProtectionPolicy = SelfProtectionPolicy.getInstance();
            selfProtectionPolicy.setExpectedHeartbeatRate(
                    selfProtectionPolicy.getExpectedHeartbeatRate() - 2);
            selfProtectionPolicy.setExpectedHeartbeatThreshold(
                    (long)(selfProtectionPolicy.getExpectedHeartbeatRate() * 0.85));
        }
    }
}
